package com.fdb.efp.nls.api.rest;

import com.fdb.basic.framework.core.constant.ApplicationConstance;
import com.fdb.basic.framework.core.constant.DataRuleType;
import com.fdb.basic.framework.core.constant.PubConstant;
import com.fdb.basic.framework.core.constant.ResponseData;
import com.fdb.basic.framework.web.controller.AbstractController;
import com.fdb.efp.console.common.ConsoleConstant;
import com.fdb.efp.e4a.service.facade.SRoleDataRuleService;
import com.fdb.efp.e4a.service.facade.SRuleCollDtlService;
import com.fdb.efp.e4a.service.facade.SRuleCollService;
import com.fdb.efp.e4a.service.vo.SRuleCollDtlVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class SRuleCollDtlController extends AbstractController  {

	@Autowired
	@Qualifier("sRuleCollService")
	private SRuleCollService sRuleCollService;
	@Autowired
	@Qualifier("sRuleCollDtlService")
	private SRuleCollDtlService sRuleCollDtlService;
	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;


	
	/**
	 * 列表数据查询
	 * @param vo
	 * @return
	 */
	@RequestMapping(value="/s/rule/coll/dtls", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<SRuleCollDtlVO>> querySRuleCollDtlAll(@RequestBody SRuleCollDtlVO vo) {		
		// 根据用户角色获取用户权限方案
		List<SRuleCollDtlVO> outputVo = null;
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);

		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
		case Owner:
			outputVo = sRuleCollDtlService.queryAllOwner(vo);
			break;
		case CurrOrg:
			outputVo = sRuleCollDtlService.queryAllCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = sRuleCollDtlService.queryAllCurrDownOrg(vo);
			break;
		default:
			outputVo = sRuleCollDtlService.queryAllOwner(vo);
			break;
		}
		return getResponseData(outputVo);
	}
	
	/**
	 * 根据规则集ID查询
	 * @param vo
	 * @return
	 */
	@RequestMapping(value="/s/rule/coll/dtls-collid/", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<SRuleCollDtlVO>> querySRuleCollDtlAllByCollId(@RequestBody SRuleCollDtlVO vo) {
		//查询是否有数据权限
		Map<String, String> key = new HashMap<>();
		key.put("ruleCollId", vo.getRuleCollId());

		boolean checkRLT = this.checkOptionalAuthority(key, ConsoleConstant.URL_DATA_PERMISS_RULE_COLL, this.sRuleCollService);
		if(!checkRLT)
			return ResponseData.create(null, ApplicationConstance.NO_AUTHORITY, ApplicationConstance.NO_AUTHORITY_MESSAGE, false);
		List<SRuleCollDtlVO> outputVo = this.sRuleCollDtlService.querySRuleCollDtlAllByCollId(vo);
		return getResponseData(outputVo);
	}
	
	/**
	 * 根据主键查询详情
	 * @return
	 */
	@RequestMapping(value="/s/rule/coll/dtl/{dtlId}", method=RequestMethod.GET)
	public @ResponseBody ResponseData<SRuleCollDtlVO> queryByPk(@PathVariable("dtlId") String dtlId) {		
		SRuleCollDtlVO inVo = new SRuleCollDtlVO();
				inVo.setDtlId(dtlId);
		SRuleCollDtlVO outputVo = sRuleCollDtlService.queryByPk(inVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键删除信息
	 * @param inSRuleCollDtlVo
	 * @return
	 */
	@RequestMapping(value="/s/rule/coll/dtl", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody SRuleCollDtlVO inSRuleCollDtlVo) {		
		int outputVo = sRuleCollDtlService.deleteByPk(inSRuleCollDtlVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键更新信息
	 * @param inSRuleCollDtlVo
	 * @return
	 */
	@RequestMapping(value="/s/rule/coll/dtl", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody SRuleCollDtlVO inSRuleCollDtlVo) {		
		int outputVo = sRuleCollDtlService.updateByPk(inSRuleCollDtlVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 新增数据
	 * @param inSRuleCollDtlVo
	 * @return
	 */
	@RequestMapping(value="/s/rule/coll/dtl", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertSRuleCollDtl(@RequestBody SRuleCollDtlVO inSRuleCollDtlVo) {
		int outputVo = sRuleCollDtlService.insertSRuleCollDtl(inSRuleCollDtlVo);
		return getResponseData(outputVo);
		
	}
	
	
	/**
	 * 新增规则及其对应参数
	 * @param inSRuleCollDtlVo
	 * @return
	 */
	@RequestMapping(value="/s/rule/coll/dtls/src", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertsInitRuleAndParamByRuleIds(@RequestBody SRuleCollDtlVO inSRuleCollDtlVo) {
		int outputVo = sRuleCollDtlService.insertsInitRuleAndParamByRuleIds(inSRuleCollDtlVo);
		return getResponseData(outputVo);
		
	}
}
